﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Graphics.PackedVector;



namespace WindowsGame
{
	public class BulletsRenderer
	{
		BulletResources bulletResources;

		IGameLogicService gameLogicService;

		GraphicsDevice graphicsDevice;



		public BulletsRenderer(GraphicsDevice graphicsDeviceIn, BulletResources bulletResourcesIn,
			IGameLogicService gameLogicServiceIn)
		{
			graphicsDevice = graphicsDeviceIn;
			bulletResources = bulletResourcesIn;
			gameLogicService = gameLogicServiceIn;
		}



		public void LoadContent()
		{
		}



		public void Draw(GameTime gameTime)
		{
			Debug.Assert(graphicsDevice != null);
			Debug.Assert(bulletResources != null);
			Debug.Assert(gameLogicService != null);

			foreach(var b in gameLogicService.GameState_.player.bullets.Value)
				foreach(ModelMesh mesh in bulletResources.model.Meshes)
				{
					foreach(BasicEffect effect in mesh.Effects)
					{
						effect.View = gameLogicService.GameState_.camera.viewMatrix.Value.GetNative();
						effect.Projection = gameLogicService.ProjectionMat;
						effect.World = b.rotationMat.GetNative() * Matrix.CreateTranslation(b.position.GetNative());
						effect.DirectionalLight0.SpecularColor = new Vector3(0.2f, 0.2f, 0.2f);
						effect.AmbientLightColor = new Vector3(0.8f, 0.8f, 0.8f);
					}
					mesh.Draw();
				}

			foreach(var enemy in gameLogicService.GameState_.enemies.Value)
				foreach(var b in enemy.data.bullets.Value)
					foreach(ModelMesh mesh in bulletResources.model.Meshes)
					{
						foreach(BasicEffect effect in mesh.Effects)
						{
							effect.View = gameLogicService.GameState_.camera.viewMatrix.Value.GetNative();
							effect.Projection = gameLogicService.ProjectionMat;
							effect.World = b.rotationMat.GetNative() * Matrix.CreateTranslation(b.position.GetNative());
							effect.DirectionalLight0.SpecularColor = new Vector3(0.2f, 0.2f, 0.2f);
							effect.AmbientLightColor = new Vector3(0.8f, 0.8f, 0.8f);
						}
						mesh.Draw();
					}
		}
	}
}